﻿using DFGK.IService.LY;
using DFGK.Service;
using DFGK_WMS.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;

namespace DFGK_WMS.WebAPI.Controllers.PC.装备大数据统计
{
    /// <summary>
    /// 装备大数据统计模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class EquipmentDataController : BaseController
    {
        private readonly IEquipmentDataStatisticsService _equipmentDataStatisticsService;


        public EquipmentDataController(IWebHostEnvironment environment, IEquipmentDataStatisticsService equipmentDataStatisticsService) : base(environment)
        {
            _equipmentDataStatisticsService = equipmentDataStatisticsService;

        }
        /// <summary>
        /// 1.装备数量占比统计
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEquipment(int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = _equipmentDataStatisticsService.GetEquipment(pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }


        /// <summary>
        /// 装备数量占比统计导出
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownEquipmentAllocate(int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = _equipmentDataStatisticsService.GetEquipment( pageIndex, pageSize, out var totalCount);
            if (list != null)
            {
                return InvDownEquipment(list);
            }
            return Ok();
        }
        /// <summary>
        /// 2.装备数量增减统计
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEquipmentChangeStatistics(
    string? equipmentType, DateTime? startDate, DateTime? endDate,
    int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = _equipmentDataStatisticsService.GetEquipmentChangeStatistics(equipmentType, startDate, endDate, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 装备数量增减导出
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownEquipmentChangeStatistics(string? equipmentType, DateTime? startDate, DateTime? endDate, int pageIndex, int pageSize)

        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = _equipmentDataStatisticsService.GetEquipmentChangeStatistics(equipmentType, startDate, endDate, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return InvDownEquipmentChangeStatistics(list);
            }
            return Ok();
        }



        /// <summary>
        /// 3.装备按组织单位统计
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUnitEquipments(int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = _equipmentDataStatisticsService.GetUnitEquipments(pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 装备按组织单位统计导出
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownUnitEquipments(int pageIndex, int pageSize)

        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = _equipmentDataStatisticsService.GetUnitEquipments(pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return InvDownUnitEquipments(list);
            }
            return Ok();
        }





        /// <summary>
        /// 4.装备大数据统计分析
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEquipmentStatistics(DateTime? startDate, DateTime? endDate, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = _equipmentDataStatisticsService.GetEquipmentStatistics(startDate, endDate, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 装备大数据统计分析导出
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownEquipmentStatistics(DateTime? startDate, DateTime? endDate, int pageIndex, int pageSize)

        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = _equipmentDataStatisticsService.GetEquipmentStatistics(startDate, endDate, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return InvDownEquipmentStatistics(list);
            }
            return Ok();
        }






        /// <summary>
        /// 5.车辆数量占比统计
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVehicleCategoryStats(int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = _equipmentDataStatisticsService.GetVehicleCategoryStats(pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }


        /// <summary>
        /// 车辆数量占比统计导出
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownVehicleCategoryStats(int pageIndex, int pageSize)

        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = _equipmentDataStatisticsService.GetVehicleCategoryStats(pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return InvDownVehicleCategoryStats(list);
            }
            return Ok();
        }






        /// <summary>
        /// 6.车辆质量状态统计分析
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVehicleStatusStats(int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = _equipmentDataStatisticsService.GetVehicleStatusStats(pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }




        /// <summary>
        /// 车辆质量状态统计分析导出
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownVehicleStatusStats(int pageIndex, int pageSize)

        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = _equipmentDataStatisticsService.GetVehicleStatusStats(pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return InvDownVehicleStatusStats(list);
            }
            return Ok();
        }




        /// <summary>
        /// 7.车辆按组织单位统计
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVehicleOrganizationStats(int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = _equipmentDataStatisticsService.GetVehicleOrganizationStats(pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }







        /// <summary>
        /// 车辆按组织单位统计导出
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownVehicleOrganizationStats(int pageIndex, int pageSize)

        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = _equipmentDataStatisticsService.GetVehicleOrganizationStats(pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return InvDownVehicleOrganizationStats(list);
            }
            return Ok();
        }



        /// <summary>
        /// 8.车辆采购数据统计分析
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVehiclePurchaseStats(int pageIndex, int pageSize, DateTime? startDate = null, DateTime? endDate = null)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = _equipmentDataStatisticsService.GetVehiclePurchaseStats(pageIndex, pageSize, out totalCount, startDate, endDate);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }




        /// <summary>
        /// 车辆采购数据统计分析导出
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownVehiclePurchaseStats(int pageIndex, int pageSize, DateTime? startDate = null, DateTime? endDate = null)

        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = _equipmentDataStatisticsService.GetVehiclePurchaseStats(pageIndex, pageSize, out totalCount, startDate, endDate);
            if (list != null)
            {
                return InvDownVehiclePurchaseStats(list);
            }
            return Ok();
        }


        /// <summary>
        /// 9.组织单位地图定位统计展示
        /// </summary>
        /// <param name="regionName"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOrganizationAndStatisticsByRegion(string? regionName, DateTime? date = null)
        {
            var list = _equipmentDataStatisticsService.GetOrganizationAndStatisticsByRegion(regionName, date);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200"));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500"));
        }


        /// <summary>
        /// 组织单位地图定位统计展示导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownOrganizationAndStatisticsByRegion(string? regionName, DateTime? date = null)

        {
           
           
            var list = _equipmentDataStatisticsService.GetOrganizationAndStatisticsByRegion(regionName, date);
            if (list != null)
            {
                return InvDownOrganizationAndStatisticsByRegion(list);
            }
            return Ok();
        }





        /// <summary>
        /// 10.库房装备数量统计分析
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEquipmentAnalysis(DateTime? startDate = null, DateTime? endDate = null)
        {
            var list = _equipmentDataStatisticsService.GetEquipmentAnalysis(startDate, endDate);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200"));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500"));
        }




        /// <summary>
        /// 库房装备数量统计分析导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InvDownEquipmentAnalysis(DateTime? startDate = null, DateTime? endDate = null)

        {


            var list = _equipmentDataStatisticsService.GetEquipmentAnalysis(startDate, endDate);
            if (list != null)
            {
                return InvDownEquipmentAnalysis(list);
            }
            return Ok();
        }


    }
}
